﻿using Microsoft.AspNetCore.Mvc;
using RobosimAPI.Class;

namespace RobosimAPI.Controllers
{
	[Route("back/Api2/Client/themeRuleList")]
	public class GetThemeRuleListController : Controller
	{
		[HttpPost]
		public IActionResult GetThemeRuleList([FromForm] ThemeRuleModel model)
		{
			string sign = HttpContext.Request.Headers["Sign"].ToString();
			string date = HttpContext.Request.Headers["Request-Date"].ToString();
			if (string.IsNullOrEmpty(sign) || string.IsNullOrEmpty(date))
			{
				return Ok(new ErrorResponse() { status = -1, errmsg = "_ERROR_SIGN_" });
			}
			Account account = new Account(model.user_id);
			if (!account.SingnCheck(sign, date))
			{
				return Ok(new ErrorResponse() { status = -1, errmsg = "_ERROR_SIGN_" });
			}
			ThemeRule[] themeRuleModels = ThemeRule.GetThemeRulesWhereThemeId(model.theme_id);
			ThemeRuleListResponse response = new ThemeRuleListResponse();
			response.status = 1;
			response.rule = new List<RuleItem>();
			foreach (ThemeRule themeRule in themeRuleModels)
			{
				RuleItem ruleItem = new RuleItem();
				ruleItem.id = themeRule.id;
				ruleItem.title = themeRule.title;
				ruleItem.version = themeRule.version;
				response.rule.Add(ruleItem);
			}
			return Ok(response);
		}
		public class ThemeRuleModel
		{
			public int user_id { get; set; }
			public int theme_id { get; set; }
		}
		public class RuleItem
		{
			public int id { get; set; }
			public string title { get; set; }
			public string version { get; set; }
		}
		public class ThemeRuleListResponse
		{
			public int status { get; set; }
			public List<RuleItem> rule { get; set; }
		}

	}
}
